from flask import Flask, render_template
#导入模块
import pymysql
from flask import jsonify
import config
import re

app = Flask(__name__)
app.config.from_object(config)
# 注册接口
def connect():
    # 连接MySQL数据库
    try:
        db = pymysql.connect(
            host='slave1',
            port=3306,
            user='root',
            passwd='root',
            db='storm1',
            charset='utf8'
            )
        #print("连接成功")
        return db
    except Exception:
        raise Exception("数据库连接失败")


#查询记录order1表中的所有信息
book_top=''
press_top=''
book_type=''
book_num=''
press_num=''
book_type_num=''
total_sum1=0
def find_all_order():
    global book_top,press_top,book_type,book_num,press_num,book_type_num
    db = connect()
    cursor = db.cursor()
    sql = "SELECT * FROM order_1;"
    try:
        cursor.execute(sql)
        result = cursor.fetchall()  # 返回的是元组,一次性获取所有数据
        #print('查询结果：', result[len(result)-1])
        result_1=result[len(result)-1]
        total_sum1=len(result)
        book_top = result_1[0]
        press_top = result_1[1]
        book_type = result_1[2]
        book_num = result_1[3]
        press_num = result_1[4]
        book_type_num = result_1[5]
        #print(book_type_num)
    except Exception:
        db.rollback()
        print("查询失败")

# 查询记录order_detail表中的所有信息
total_money=0
total_sum=0
province=''
top_pro=''
top_pro_sum=''
pay=''
pay_sum=''
def find_all_order_detail():
    global total_money,total_sum,province,top_pro,top_pro_sum,pay,pay_sum
    db = connect()
    cursor = db.cursor()
    sql = "SELECT * FROM order_detail;"
    try:
        cursor.execute(sql)
        result = cursor.fetchall()  # 返回的是元组,一次性获取所有数据
        #print('查询结果：', result[len(result)-1])
        result_1=result[len(result)-1]
        #print(type(result_1))
        total_money = int(result_1[0])
        total_sum = int(result_1[1])
        province = result_1[2]
        top_pro = result_1[3]
        top_pro_sum = result_1[4]
        pay = result_1[5]
        pay_sum = result_1[6]
        #print(pay_sum)
    except Exception:
        db.rollback()
        print("查询失败")

@app.route('/')
def indexs():
    return render_template("main.html")

#接下首先处理order_detail中的数据
#实现省份销量排名
@app.route('/pro_top')
def pro_top():
    find_all_order_detail()
    global top_pro,top_pro_sum
    w = top_pro[1:-1]
    w1 = w.split(',')

    v = top_pro_sum[1:-1]
    v1 = v.split(',')
    #print(v1)
    v2 = list(map(int, v1))
    #print(w1,v2)
    return jsonify({"data": w1, "value": v2})

#实现书籍排名
@app.route('/book_top')
def book_top():
    find_all_order()
    global book_top,book_num
    re1=[]
    w = book_top[1:-1]
    w1 = w.split(',')
    s = "[\n`~!：―@#$%^&*()+-=|{}':;>/?~！@#￥%……&*（）()+|{}‘；：”“’。， 、？]"
    for i in w1:
        name = re.split(s, i)
        re1.append(name[1])
    v = book_num[1:-1]
    v1 = v.split(',')
    v2 = list(map(int, v1))
    return jsonify({"data": re1, "value": v2})

#实现出版社排名
@app.route('/presstop')
def press_top():
    find_all_order()
    global press_top,press_num
    w = press_top[1:-1]
    w1 = w.split(',')

    v = press_num[1:-1]
    v1 = v.split(',')
    v2 = list(map(int, v1))
    return jsonify({"data": w1, "value": v2})

#统计总金额、订单数目、下单客户数
@app.route("/c1")
def get_c1_data():
    find_all_order()
    find_all_order_detail()
    global total_money,total_sum,total_sum
    data=[]
    data.append(total_money)
    data.append(total_sum)
    data.append(total_sum)
    return jsonify({"total_price": data[0], "order_num": data[1], "recipient": data[2]})
    # jsonify({"total_price":data[0],"order_num":data[1],"recipient":data[2]})


#实现地图展示各个省份的销量信息
@app.route('/map1')
def map1():
    find_all_order_detail()
    global province
    w = province[1:-1]
    w1 = w.split(',')
    rs1 = []
    rs2 = []
    for i in w1:
        w2 = i.split('=')
        rs1.append(w2[0])
        rs2.append(w2[1])
    rs2 = list(map(int, rs2))
    rs3=[]
    for i in range(len(rs1)):
        rs3.append({"name": rs1[i].replace(" ",""), "value": int(rs2[i])})
    #print(rs3)
    return jsonify({"data": rs3})

a=0
b=0
#实现计算速度
@app.route('/speed')
def speed():
    find_all_order_detail()
    find_all_order()
    global total_sum,b,a,total_sum1
    rs1 = []
    rs1.append(int(int(total_sum)-int(b))+int(total_sum1)-int(a))
    b=total_sum
    a=total_sum1
    rs1.append(int((a+b)))
    print(int(rs1[0]))
    return jsonify({"data": int(rs1[0])})

#实现书籍类型统计
@app.route('/book_type')
def book_type():
    find_all_order()
    global book_type,book_type_num
    w = book_type[1:-1]
    w1 = w.split(',')

    v = book_type_num[1:-1]
    v1 = v.split(',')
    v2 = list(map(int, v1))
    re1=[]
    for i in range(len(w1)):
        re1.append({"name": w1[i],"value": int(v2[i])})
    return jsonify({"data": re1})

#实现下单方式统计
@app.route('/pay')
def pay():
    find_all_order_detail()
    global pay,pay_sum
    w = pay[1:-1]
    w1 = w.split(',')

    v = pay_sum[1:-1]
    v1 = v.split(',')
    v2 = list(map(int, v1))
    re1=[]
    for i in range(len(w1)):
        re1.append({"name": w1[i], "value": int(v2[i])})
    return jsonify({"data": re1})

if __name__=="__main__":
    app.run()